#!/usr/bin/env python
# -*- coding:utf-8 -*-
# file:do_pickle_to_input.py
# author:肖宇轩
# datetime:2024-11-12 20:02
# software: VsCode

"""
利用字典解析式，生成记录一组学生2门课的成绩的字典列表，将这个字典列表序列化以后，保存到一个文件中（列表中的每一个元素，保存为文件中的一行）；
"""

import random
import string
import pickle
def random_name():
    return ''.join(random.choices(string.ascii_lowercase, k=3)) 


num_students = 5 
students = [
    {
        "StudentName": random_name(),
        "english": random.randint(0, 100),
        "math": random.randint(0, 100)
    }
    for _ in range(num_students)
]


with open("student_scores.txt", "wb") as file:
    for student in students:
        pickle.dump(student, file)

print("学生成绩已保存到文件 student_scores.txt 中")

